//
//  KXPopMenu.swift
//  开心微博
//
//  Created by 郑开心 on 15/12/6.
//  Copyright © 2015年 郑开心. All rights reserved.
//

import UIKit

class KXPopMenu: UIImageView {

    var contentView : UIView!{
        didSet{
            if self.contentView != nil{
                self.contentView.removeFromSuperview()
            }
            contentView.backgroundColor = UIColor.clearColor()
            self.addSubview(self.contentView)
        }
    }
    
    class func hide(){
        for popMenu in UIApplication.sharedApplication().keyWindow!.subviews {
            if popMenu.isKindOfClass(KXPopMenu){
                popMenu.removeFromSuperview()
            }
        }
    }
    
    class func showInRect(rect: CGRect) -> KXPopMenu{
        let menu = KXPopMenu(frame: rect)
        menu.userInteractionEnabled = true
        
        menu.image = UIImage.imageWithStretchableName("popover_background")
        UIApplication.sharedApplication().keyWindow?.addSubview(menu)
        return menu
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        let y : CGFloat = 9
        let margin : CGFloat = 5
        let x = margin
        let w = self.width - 2 * margin
        let h = self.height - y - margin
        
        contentView.frame = CGRectMake(x, y, w, h);
    }
    
}
